package za.um.td.entity.level;

import java.net.URL;
import java.util.logging.Logger;

import za.um.td.util.XMLUtil;

import com.jme.entity.Entity;
import com.jme.math.Vector3f;
import com.jme.scene.Node;

public class Terrain extends AbstractTerrain
{
	private static final Logger LOG = Logger.getLogger(Entity.class.getName());
	
	private Node terrain = null;
	
	private Tile tile = null;
    
	public Terrain(String id) {
		super(id);
	}
	
	public Terrain(String id, Vector3f terrainPosition, Vector3f terrainScale, float heightScale) {
		super(id);
		init(terrainPosition, terrainScale, heightScale);
	}
	
	private void init(Vector3f terrainPosition, Vector3f terrainScale, float heightScale)
	{
		tile = new Tile("tile_0_0");
		
		URL xmlUrl=XMLUtil.class.getClassLoader().getResource("za/um/td/data/level/tile/"+tile.getId()+".xml");
		
		tile.init(xmlUrl,terrainPosition,terrainScale, heightScale);
		
		terrain = new Node();
		this.setSpatial(terrain);
		
		terrain.attachChild(tile.getSpatial());
		
		terrain.setIsCollidable(true);
		
		initAbstractTerrainMembers();
	}
	
	public float getHeight(Vector3f location) {
		return tile.getHeight(location);
	}

	@Override
	public Vector3f getCentreLocation() {
		return tile.getCentreLocation();
	}

	@Override
	public int getNumBlocksX() {
		return (int)getXLength();
	}

	@Override
	public int getNumBlocksZ() {
		return (int)getZLength();
	}

	@Override
	public float getXLength() {
		return tile.getTerrainPage().getTotalSize()*tile.getTerrainScale().x;
	}

	@Override
	public float getZLength() {
		return tile.getTerrainPage().getTotalSize()*tile.getTerrainScale().z;
	}
	
	@Override
	public Vector3f getScale()
	{
		return tile.getTerrainScale();
	}
}
